package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.annotation.SystemServiceLog;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootChip;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootChipSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootChipMapper;
import org.footballmanager.pandafootball.system.service.FootChipService;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootChipServiceImpl extends ServiceImpl<FootChip> implements FootChipService {
    /*白色	75以下	 5个碎片随机合成一个
          绿色	75-79	10个碎片随机合成一个
          蓝色	80-83	15个碎片随机合成一个
          紫色	84-89	20个碎片随机合成一个
          橙色	90-100	30个碎片随机合成一个*/
    private static final long WHITE = 5;
    private static final long GREEN = 10;
    private static final long BLUE = 15;
    private static final long PURPLE = 20;
    private static final long ORANGE = 30;

    @Autowired
    private FootPersonService footPersonService;
    @Autowired
    private FootChipMapper footChipMapper;
    @Autowired
    private FootZnxService footZnxService;
    @Autowired
    private FootUserService footUserService;

    /**
     * 更新碎片记录
     *
     * @param entity  实体类
     * @param request 请求
     * @return 是否成功
     */
    @Override
    @Transactional
    @SystemServiceLog(description = "更新非空碎片记录")
    public int updateNotNull(FootChip entity, HttpServletRequest request) {
        if (null != entity.getContext()) {
            footZnxService.sendZnx(footUserService.getAdminUserid(), entity.getUserId(),
                    entity.getContext(), null, null, request);
        }
        return super.updateNotNull(entity, request);
    }

    /**
     * 根据所给用户主键，随机获得一个洲际球星
     *
     * @param userId  用户主键
     * @param request 请求
     * @return 洲际球星名称
     */
    @Override
    @SystemServiceLog(description = "根据所给用户主键，随机获得一个洲际球星")
    public FootPerson randomBluePerson(Long userId, HttpServletRequest request) {
        FootChip chip = this.findChipByUserId(userId);
        if (chip.getHaveBlue() >= BLUE) {
            chip.setHaveBlue(chip.getHaveBlue() - BLUE);
            this.updateNotNull(chip, request);
            return footPersonService.findPersonByScore("80", "83", userId, request);
        }
        return null;
    }

    /**
     * 根据所给用户主键，随机获得一个普通球星
     *
     * @param userId  用户主键
     * @param request 请求
     * @return 普通球星名称
     */
    @Override
    @SystemServiceLog(description = "根据所给用户主键，随机获得一个普通球星")
    public FootPerson randomGreenPerson(Long userId, HttpServletRequest request) {
        FootChip chip = this.findChipByUserId(userId);
        if (chip.getHaveGreen() >= GREEN) {
            chip.setHaveGreen(chip.getHaveGreen() - GREEN);
            this.updateNotNull(chip, request);
            return footPersonService.findPersonByScore("75", "79", userId, request);
        }
        return null;
    }

    /**
     * 根据所给用户主键，随机获得一个顶级球星
     *
     * @param userId  用户主键
     * @param request 请求
     * @return 顶级球星名称
     */
    @Override
    @SystemServiceLog(description = "根据所给用户主键，随机获得一个顶级球星")
    public FootPerson randomOrangePerson(Long userId, HttpServletRequest request) {
        FootChip chip = this.findChipByUserId(userId);
        if (chip.getHaveOrange() >= ORANGE) {
            chip.setHaveOrange(chip.getHaveOrange() - ORANGE);
            this.updateNotNull(chip, request);
            return footPersonService.findPersonByScore("90", null, userId, request);
        }
        return null;
    }

    /**
     * 根据所给用户主键，随机获得一个超级球星
     *
     * @param userId  用户主键
     * @param request 请求
     * @return 超级球星名称
     */
    @Override
    @SystemServiceLog(description = "根据所给用户主键，随机获得一个超级球星")
    public FootPerson randomPurplePerson(Long userId, HttpServletRequest request) {
        FootChip chip = this.findChipByUserId(userId);
        if (chip.getHavePurple() >= PURPLE) {
            chip.setHavePurple(chip.getHavePurple() - PURPLE);
            this.updateNotNull(chip, request);
            return footPersonService.findPersonByScore("84", "89", userId, request);
        }
        return null;
    }

    /**
     * 根据所给用户主键，随机获得一个白人
     *
     * @param userId  用户主键
     * @param request 请求
     * @return 白人名称
     */
    @Override
    @SystemServiceLog(description = "根据所给用户主键，随机获得一个白人")
    public FootPerson randomWhiltePerson(Long userId, HttpServletRequest request) {
        FootChip chip = this.findChipByUserId(userId);
        if (chip.getHaveWhile() >= WHITE) {
            chip.setHaveWhile(chip.getHaveWhile() - WHITE);
            this.updateNotNull(chip, request);
            return footPersonService.findPersonByScore("0", "74", userId, request);
        }
        return null;
    }

    /**
     * 根据用户主键获取碎片数量
     *
     * @param userId 用户主键
     * @return 用户碎片实体类
     */
    @Override
    @SystemServiceLog(description = "根据用户主键获取碎片数量")
    public FootChip findChipByUserId(Long userId) {
        Example example = new Example(FootChip.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(Constants.USERID, userId);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 获取对应类型碎片
     *
     * @param userId  用户主键
     * @param type    类型
     * @param num     数量
     * @param request 请求
     */
    @Override
    @SystemServiceLog(description = "获取对应类型碎片")
    public void randomGetChip(Long userId, int type, int num, HttpServletRequest request) {
        FootChip chip = this.findChipByUserId(userId);
        if (null != chip) {
            switch (type) {
                case 1:
                    chip.setHaveWhile(chip.getHaveWhile() + num);
                    break;
                case 2:
                    chip.setHaveGreen(chip.getHaveGreen() + num);
                    break;
                case 3:
                    chip.setHaveBlue(chip.getHaveBlue() + num);
                    break;
                case 4:
                    chip.setHavePurple(chip.getHavePurple() + num);
                    break;
                case 5:
                    chip.setHaveOrange(chip.getHaveOrange() + num);
                    break;
                default:
                    break;
            }
        }
        this.updateNotNull(chip, request);
    }

    /**
     * 新增默认碎片信息
     *
     * @param user    用户
     * @param request 请求
     */
    @Override
    @SystemServiceLog(description = "新增默认碎片信息")
    public void saveNewChip(FootUser user, HttpServletRequest request) {
        FootChip chip = new FootChip();
        chip.setUserId(user.getId());
        chip.setHaveWhile(0L);
        chip.setHaveGreen(0L);
        chip.setHaveBlue(0L);
        chip.setHavePurple(0L);
        chip.setHaveOrange(0L);
        this.save(chip, request);
    }

    /**
     * 根据条件查找碎片信息列表
     *
     * @param footChipSearchForm 条件
     * @return 列表
     */
    @Override
    @SystemServiceLog(description = "根据条件查找碎片信息列表")
    public List<FootChip> findall(FootChipSearchForm footChipSearchForm) {
        return footChipMapper.findall(footChipSearchForm);
    }

    /**
     * 根据碎片主键查找碎片信息
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    @SystemServiceLog(description = "根据碎片主键查找碎片信息")
    public FootChip get(Serializable id) {
        return footChipMapper.findById(id);
    }

    /**
     * 根据条件查询碎片信息列表
     *
     * @param entityname         实体类名
     * @param map                参数
     * @param footChipSearchForm 条件
     */
    @Override
    @SystemServiceLog(description = "根据条件查询碎片信息列表")
    public void search(String entityname, Model map, FootChipSearchForm footChipSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footChipSearchForm);
        Page<FootChip> page = PageHelper.startPage(footChipSearchForm.getPageIndex(), footChipSearchForm.getPageSize());
        List<FootChip> list = this.findall(footChipSearchForm);
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
        map.addAttribute(Constants.PI, page.toPageInfo());
    }

    /**
     * 根据用户主键删除碎片相关信息
     *
     * @param userId  用户主键
     * @param request 请求
     */
    @Override
    @SystemServiceLog(description = "根据用户主键删除碎片相关信息")
    public void deleteByUserId(Long userId, HttpServletRequest request) {
        FootChipSearchForm footChipSearchForm = new FootChipSearchForm();
        footChipSearchForm.setUserId(userId);
        List<FootChip> list = this.findall(footChipSearchForm);
        if (list != null && !list.isEmpty()) {
            for (FootChip footChip : list) {
                this.delete(footChip.getId(), request);
            }
        }
    }
}
